<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "mkt_chat".
 *
 * @property string $id
 * @property string $sender_id
 * @property string $recver_id
 * @property string $content
 * @property integer $is_read
 * @property integer $is_deleted
 * @property string $create_time
 * @property string $update_time
 */
class MktChat extends \common\wosotech\base\ActiveRecord
{
    const CAT_ADD_FRIEND = 0;
    const CAT_FRIEND_MESSAGE = 1;
    const CAT_SYSTEM_MESSAGE = 2;

    const APPROVE_STATUS_WAIT = 0;
    const APPROVE_STATUS_ACCEPT = 1;
    const APPROVE_STATUS_IGNORE = 2;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'mkt_chat';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['sender_id', 'recver_id', 'is_read', 'is_deleted', 'cat', 'approved'], 'integer'],
            [['content'], 'string'],
            [['create_time', 'update_time'], 'safe']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'cat' => '类型 ',    // 0:好友申请, 1:好友消息
            'sender_id' => '发送者ID ',    // 0:系统
            'recver_id' => '接收者ID',
            'content' => '内容',
            'is_read' => '已阅',            // 是否已阅好友消息
            'is_deleted' => '已删',
            'create_time' => '创建时间',
            'update_time' => '阅读时间',
            'approved' => '同意加好友',      // 是否处理好友申请, 0:待处理, 1:同意加好友, 2: 忽视
        ];
    }

    /**
     * @inheritdoc
     * @return MktChatQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new MktChatQuery(get_called_class());
    }

    public function getSender()
    {
        return $this->hasOne(MktUser::className(), ['id' => 'sender_id']);
    }

    public function getRecver()
    {
        return $this->hasOne(MktUser::className(), ['id' => 'recver_id']);
    }

    public static function getCatOption($key = null)
    {
        $arr = array(
            static::CAT_SYSTEM_MESSAGE => '系统消息',
            static::CAT_ADD_FRIEND => '申请加好友',
            static::CAT_FRIEND_MESSAGE => '好友私信');
        return $key === null ? $arr : (isset($arr[$key]) ? $arr[$key] : '');
    }

    public static function getApproveStatusOption($key = null)
    {
        $arr = array(
            static::APPROVE_STATUS_WAIT => '等待处理',
            static::APPROVE_STATUS_ACCEPT => '接受',
            static::APPROVE_STATUS_IGNORE => '忽视');
        return $key === null ? $arr : (isset($arr[$key]) ? $arr[$key] : '');
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
        if ((!$insert) && isset($changedAttributes['approved'])) {            
            if ($changedAttributes['approved'] == self::APPROVE_STATUS_WAIT) {
                if ($this->approved == self::APPROVE_STATUS_ACCEPT) {
                    $this->recver->allowFriend($this->sender->id);
                }                
            }
            if ($changedAttributes['approved'] == self::APPROVE_STATUS_WAIT && $this->approved == self::APPROVE_STATUS_IGNORE ) {
                $this->sender->addGold(Yii::$app->keyStorage->get('business.golds_per_friend', 10), MktGold::SOURCE_REASON_APPLY_FRIEND, "不接受{$this->sender->name}向{$this->recver->name}申请加好友");
            }
        }
    }

    public static function sendMessageAjax($params)
    {
        yii::error(print_r([$_GET, $params], true));
        $chat = new \common\models\MktChat;
        $chat->sender_id = $params['sender_id'];
        $chat->recver_id = $params['recver_id'];
        $chat->content = $params['content'];
        $chat->cat = self::CAT_FRIEND_MESSAGE;
        $chat->save(false);
        return \yii\helpers\Json::encode(['code' => 0, 'data' => 'OK']);
    }

    public static function ignoreAddFriendAjax($params)
    {
        yii::error(print_r([$_GET, $params], true));
        $model = MktChat::findOne($params['id']);
        $model->approved = MktChat::APPROVE_STATUS_IGNORE;
        $result = $model->save();
        if ( true !== $result) {
            //yii::error($result);
            return \yii\helpers\Json::encode(['code' => 1, 'data' => $result]);
        }
        return \yii\helpers\Json::encode(['code' => 0, 'data' => 'OK']);
    }

    public static function acceptAddFriendAjax($params)
    {
        $model = MktChat::findOne($params['id']);
        $model->approved = MktChat::APPROVE_STATUS_ACCEPT;
        $result = $model->save();
        if ( true !== $result) {
            //yii::error($result);
            return \yii\helpers\Json::encode(['code' => 1, 'data' => $result]);
        }
        return \yii\helpers\Json::encode(['code' => 0, 'data' => 'OK']);
    }


}
